# -*- coding: utf-8 -*-
# Copyright (C) 2007 Boris Dušek
# See the file LICENCE in the top-level directory of this distribution
# for licensing information.
# vim:ts=4:sw=4:et

from army.core import memory
from army.modules.memory import PagedMemoryEngine,FileMemoryEngine
import StringIO

import unittest

class PagedMemoryEngineTest(unittest.TestCase):
    
    def testWriteRead(self):
        buf = StringIO.StringIO()
        mem = PagedMemoryEngine.PagedMemoryEngine(FileMemoryEngine.FileMemoryEngine(buf))
        mem.write(0x00000000,'\x01')
        mem.write(0xffffffff,'\x02')
        cmp_string = '\x01' + (2*mem.page_size-2)*'\x00' + '\x02'
        self.assertEqual(buf.getvalue(),cmp_string)
        self.assertEqual(mem.read(0x00000000),'\x01')
        for address in range(1,mem.page_size):
            self.assertEqual(mem.read(address),'\x00')
        for address in range(0xffffffff-mem.page_size+1,0xffffffff-1):
            self.assertEqual(mem.read(address),'\x00')
        self.assertEqual(mem.read(0xffffffff),'\x02')

if __name__ == '__main__':
    unittest.main()
